package com.pxene.dmp.common;

/**
 日期类
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	/**
	 * 将输入的时间按照format的形式输出
	 */
	public static String getFormat(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	/**
	 * 将format形式的字符串时间转换成Date
	 */
	public static Date getDate(String str, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = formatter.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 获取当前时间，按format形式输出
	 */
	public static String getNowFormat(String format) {
		return getFormat(new Date(), format);
	}
	
	public static String getNowYear() {
		return getFormat(new Date(), "yyyy");
	}
	
	public static String getNowMonth() {
		return getFormat(new Date(), "MM");
	}
	
	public static String getNowDay() {
		return getFormat(new Date(), "dd");
	}
	
	public static String getNowHour() {
		return getFormat(new Date(), "HH");
	}
	
	public static String getNowMinute() {
		return getFormat(new Date(), "mm");
	}
	
	public static String getNowSecond() {
		return getFormat(new Date(), "ss");
	}
	
	public static Date addYears(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, delay);
		return calendar.getTime();
	}
	
	public static Date addMonths(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, delay);
		return calendar.getTime();
	}
	
	public static Date addDays(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, delay);
		return calendar.getTime();
	}
	
	public static Date addHours(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, delay);
		return calendar.getTime();
	}
	
	public static Date addMinutes(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, delay);
		return calendar.getTime();
	}
	
	public static Date addSeconds(Date date, int delay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, delay);
		return calendar.getTime();
	}
	
	/**
	 * 判断time是否starttime<=time<=endtime，传入的格式为format
	 */
	public static boolean isBetween(String time, String starttime, String endtime, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.getDate(time, format));
		
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(DateUtils.getDate(starttime, format));
		if(calendar.before(startCalendar)) {
			return false;
		}
		
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(DateUtils.getDate(endtime, format));
		if(calendar.after(endCalendar)) {
			return false;
		}
		
		return true;
	}
	
	public static boolean isBefore(String time, String compare, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.getDate(time, format));
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(DateUtils.getDate(compare, format));
		
		return calendar.before(compare);
	}
	
	public static boolean isAfter(String time, String compare, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.getDate(time, format));
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(DateUtils.getDate(compare, format));
		
		return calendar.after(endCalendar);
	}
	
}